from realpython.index.models import *
class VisitCount:
    def process_request(self, request):
        counter = Counter.all().get()
        if counter:
            counter.pvcount = counter.pvcount + 1
            #ip =  request.META['REMOTE_ADDR']
            #print request.COOKIES.get('ip'), '*'
            if not request.COOKIES.get('ip'):
                #print '88*'
                counter.ipcount = counter.ipcount + 1
                #return HttpResponse('hh')
        else:
            counter = Counter(pvcount=1, ipcount=1)
        counter.put()
        return None
    
    def process_response(self, request, response):
        if not request.COOKIES.get('ip'):
            ip =  request.META['REMOTE_ADDR']
            response.set_cookie('ip', ip, 86400)
        return response
        
from google.appengine.api import users as googleuser
class VisitRecord:
    def process_request(self, request):
        if request.COOKIES.get('ip'):
            return None
        visit = Visiters()
        user = googleuser.get_current_user()
        if user:
            visit.gmail = user.email()
        if request.user.is_authenticated():
            visit.username = request.user.username
        ip =  request.META['REMOTE_ADDR']
        visit.ip = ip
        visit.put()
        return None
    
    
